package com.example.tobisfirstapp;

import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class SelectImageActivity extends Activity {

	public static final String STATE_IMG_URL="img_url";
	//  TextView textTargetUri;
	  ImageView targetImage;
	  String imgPath;  
	  Button confirmImg;
	  
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		System.out.println("onCreate");
		setContentView(R.layout.activity_select_image);
		// Show the Up button in the action bar.
		setupActionBar();
		
		
		 Button buttonLoadImage = (Button)findViewById(R.id.loadimage);
      //   textTargetUri = (TextView)findViewById(R.id.targeturi);
         targetImage = (ImageView)findViewById(R.id.targetimage);

         buttonLoadImage.setOnClickListener(new Button.OnClickListener(){
			     @Override
			     public void onClick(View arg0) {
			      // TODO Auto-generated method stub
			      Intent intent = new Intent(Intent.ACTION_PICK,
			        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
			      startActivityForResult(intent, 0);
			     }});
         
         
         
         
         //continue button
         
         		confirmImg = (Button) findViewById(R.id.image_confirm);
         		confirmImg.setOnClickListener(new Button.OnClickListener(){
   			     @Override
   			     public void onClick(View arg0) {
   			      // TODO Auto-generated method stub
   			    	 if(SelectImageActivity.this.targetImage != null){
   			    	  Intent intent = new Intent(SelectImageActivity.this,MosaicActivity.class);
   	   			      intent.putExtra(STATE_IMG_URL, SelectImageActivity.this.imgPath);
   	   			      startActivityForResult(intent, 0);
   			    	 }
   			    
   			     }});
         System.out.println("end of onCreate");		
     }

    	

	/**
	 * Set up the {@link android.app.ActionBar}.
	 */
	private void setupActionBar() {

		getActionBar().setDisplayHomeAsUpEnabled(true);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.select_image, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

	    if (resultCode == RESULT_OK){
	     Uri targetUri = data.getData();
	   //  textTargetUri.setText(targetUri.toString());
	     imgPath = targetUri.toString();
	     Bitmap bitmap;
	     try {
	      bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
	      
	      targetImage.setImageBitmap(bitmap);
	      //show continue button after image has been loaded
	      confirmImg.setVisibility(View.VISIBLE);
	     } catch (FileNotFoundException e) {
	      // TODO Auto-generated catch block
	      e.printStackTrace();
	     }
	    }
    }
    
    
    public void onSaveInstanceState(Bundle savedInstanceState){
    	
    	savedInstanceState.putString(STATE_IMG_URL, imgPath);
    	
    	super.onSaveInstanceState(savedInstanceState);
    }
    
    public void onRestoreInstanceState(Bundle savedInstanceState){
        super.onRestoreInstanceState(savedInstanceState);
        
    	System.out.println("onRestore");
    	
    	imgPath = savedInstanceState.getString(STATE_IMG_URL);
    	
    	if(imgPath != null && imgPath != ""){
    		Bitmap bitmap;
	   	     try {
	   	    	 Uri imgUri = Uri.parse(imgPath);
	   	    	 bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imgUri));
	   	    	 targetImage.setImageBitmap(bitmap);
	   	    	 confirmImg.setVisibility(View.VISIBLE);
	   	     }catch(NullPointerException ne){
	   	    	 System.out.println("null pointer:");
	   	    	 ne.printStackTrace();
	   	     } catch (FileNotFoundException e) {
	   	      // TODO Auto-generated catch block
	   	    	 System.out.println("FNF Exception");
	   	      e.printStackTrace();
	   	     }catch(Exception e2){
	   	    	 e2.printStackTrace();
	   	     }
    	}
    		 
    	   System.out.println("end of onRestore"); 
    	 
    	 
    }
    
    
    
    
}
